package cn.darkhorse.mesadmin.controller.system;

import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.service.MesDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static cn.darkhorse.common.constant.CommonConstant.MES_DEVICE_ACQUISITION_YES;

/**
 * @author yuanjun
 * @since 2022/11/15 10:06
 */
@RequestMapping("/mes/dept")
@RestController
public class MesDeptController extends BaseController {
    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private MesDeviceService mesDeviceService;

    /**
     * 将部门组合成上下级并列的形式，如 **公司/**部门
     * @param type  type=1,返回组合形式，type=2返回名称和id，用于翻译部门名称
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult list(Integer type) {
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        Map<Long, String> deptMap = new HashMap<>();
        for (SysDept dept :
                sysDepts) {
            deptMap.put(dept.getDeptId(), dept.getDeptName());
        }

        if(null != type && type.equals(2)) {
           return  AjaxResult.success(deptMap);
        }

        Map<Long, String> resultMap = new HashMap<>();
        for (SysDept dept :
                sysDepts) {
            if (dept.getParentId().equals(0L)) {
                continue;
            }
            String[] split = (dept.getAncestors() + "," + dept.getDeptId().toString()).split(",");

            StringBuilder deptFullName = new StringBuilder();
            for (int i = 1; i < split.length; i++) {
                deptFullName.append(deptMap.get(Long.valueOf(split[i]))).append("/");
            }
            resultMap.put(dept.getDeptId(),deptFullName.substring(0,deptFullName.length() - 1).toString());
        }
        return AjaxResult.success(resultMap);
    }
//
//    @RequestMapping("/acquisition/list")
//    public AjaxResult acquisitionList(Integer type) {
//        List<MesDevice> deptList = mesDeviceService.list(new QueryWrapper<MesDevice>().select("DISTINCT dept").lambda()
//                .eq(MesDevice::getIsAcquisitionDevice, MES_DEVICE_ACQUISITION_YES));
//        Set<Long> deptSet = new HashSet<>(deptList.size());
//        for (MesDevice device :
//                deptList) {
//            deptSet.add(device.getDept());
//        }
//
//        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
//        Map<Long, String> deptMap = new HashMap<>();
//        Iterator<SysDept> iterator = sysDepts.iterator();
//        while (iterator.hasNext()) {
//            SysDept dept = iterator.next();
//            deptMap.put(dept.getDeptId(), dept.getDeptName());
//            if (!deptSet.contains(dept.getDeptId())) {
//                iterator.remove();
//            }
//        }
//
//        if(null != type && type.equals(2)) {
//            Map<Long, String> resultMap = new HashMap<>();
//            for (SysDept dept:sysDepts) {
//                resultMap.put(dept.getDeptId(), dept.getDeptName());
//            }
//            return  AjaxResult.success(resultMap);
//        }
//
//        Map<Long, String> resultMap = new HashMap<>();
//        for (SysDept dept :
//                sysDepts) {
//            if (dept.getParentId().equals(0L)) {
//                continue;
//            }
//            String[] split = (dept.getAncestors() + "," + dept.getDeptId().toString()).split(",");
//
//            StringBuilder deptFullName = new StringBuilder();
//            for (int i = 1; i < split.length; i++) {
//                deptFullName.append(deptMap.get(Long.valueOf(split[i]))).append("/");
//            }
//            resultMap.put(dept.getDeptId(),deptFullName.substring(0,deptFullName.length() - 1).toString());
//        }
//        return AjaxResult.success(resultMap);
//    }
//
//    @RequestMapping("/capacity/list")
//    public AjaxResult capacityList(Integer type) {
//        List<MesDevice> deptList = mesDeviceService.list(new QueryWrapper<MesDevice>().select("DISTINCT dept").lambda()
//                .eq(MesDevice::getIsCapacityDevice, MES_DEVICE_ACQUISITION_YES));
//        Set<Long> deptSet=deptList.stream().map(MesDevice::getDept).collect(Collectors.toSet());
//        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
//        Map<Long, String> deptMap = new HashMap<>();
//        Iterator<SysDept> iterator = sysDepts.iterator();
//        while (iterator.hasNext()) {
//            SysDept dept = iterator.next();
//            deptMap.put(dept.getDeptId(), dept.getDeptName());
//        }
//        if(null != type && type.equals(2)) {
//            Map<Long, String> resultMap = new HashMap<>();
//            for (SysDept dept:sysDepts) {
//                resultMap.put(dept.getDeptId(), dept.getDeptName());
//            }
//            return  AjaxResult.success(resultMap);
//        }
//
//        Map<Long, String> resultMap = new HashMap<>();
//        for (SysDept dept :
//                sysDepts) {
//            if (dept.getParentId().equals(0L)) {
//                continue;
//            }
//            String[] split = (dept.getAncestors() + "," + dept.getDeptId().toString()).split(",");
//
//            StringBuilder deptFullName = new StringBuilder();
//            for (int i = 1; i < split.length; i++) {
//                deptFullName.append(deptMap.get(Long.valueOf(split[i]))).append("/");
//            }
//            resultMap.put(dept.getDeptId(),deptFullName.substring(0,deptFullName.length() - 1).toString());
//        }
//        Map<Long, String> dataMap = new HashMap<>();
//        resultMap.entrySet().forEach(item->{
//            if (deptSet.contains(item.getKey())) {
//                dataMap.put(item.getKey(), item.getValue());
//            }
//        });
//        return AjaxResult.success(dataMap);
//    }

    @RequestMapping("/view")
    public AjaxResult view(Integer type,Long deptId) {
        if(null==deptId){
            throw new ServiceException("部门Id不能为空！");
        }
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        Map<Long, String> deptMap = new HashMap<>();
        Iterator<SysDept> iterator = sysDepts.iterator();
        while (iterator.hasNext()) {
            SysDept dept = iterator.next();
            deptMap.put(dept.getDeptId(), dept.getDeptName());
        }
        if(null != type && type.equals(2)) {
            Map<Long, String> resultMap = new HashMap<>();
            for (SysDept dept:sysDepts) {
                resultMap.put(dept.getDeptId(), dept.getDeptName());
            }
            return  AjaxResult.success(resultMap);
        }

        Map<Long, String> resultMap = new HashMap<>();
        for (SysDept dept :
                sysDepts) {
            if (dept.getParentId().equals(0L)) {
                continue;
            }
            String[] split = (dept.getAncestors() + "," + dept.getDeptId().toString()).split(",");

            StringBuilder deptFullName = new StringBuilder();
            for (int i = 1; i < split.length; i++) {
                deptFullName.append(deptMap.get(Long.valueOf(split[i]))).append("/");
            }
            resultMap.put(dept.getDeptId(),deptFullName.substring(0,deptFullName.length() - 1).toString());
        }
        Map<Long, String> dataMap = new HashMap<>();
        resultMap.entrySet().forEach(item->{
            if (deptId.equals(item.getKey())) {
                dataMap.put(item.getKey(), item.getValue());
            }
        });
        return AjaxResult.success(dataMap.get(deptId));
    }
}
